<?php
$id         = gpc_getIntPost('id',$id);
$Name		= gpc_getStringPost("Name");
$Type		= gpc_getStringPost("Type");
$listorder	= gpc_getStringArray("listorder");
$listid		= gpc_getStringArray("listid");
$listvalue1	= gpc_getStringArray("listvalue1");
$listvalue2	= gpc_getStringArray("listvalue2");
$idP        = gpc_getIntPost('idP');

if($act=='insertfilter')
{
    //insert parent:
	$sql_insert="insert into tblproduct_filter 
				(iCategoryId,iParentId,cName,cValue) 
		values($idP,0,'$Name','$Type')";
		
	$dbsql->query($sql_insert);
	
	//select Id :
	$idParent  = $dbsql->insert_id();
	
	//insert filter value:
	
	for($i=0;$i<count($listvalue1);$i++)
	{
		$vl1=trim($listvalue1[$i]);
		$vl2=trim($listvalue2[$i]);
		
		if(empty($vl1) || (empty($vl2) && $Type=="Price"))continue;
		$value=($Type=="Price")? $vl1."-".$vl2 : $vl1;
		
		$sql_insert="insert into tblproduct_filter 
				(iCategoryId,iParentId,cName,cValue) 
		values($idP,$idParent,'$value','')";
		
		$dbsql->query($sql_insert);
	}
}
else if($id>0)
{
    //update parent:
    $sql_update="update tblproduct_filter set
    			cName	= '$Name',
    			cValue	= '$Type'
    			where iId=$id";
    	
    $dbsql->query($sql_update);	
    
    //update filter value:
    
    for($i=0;$i<count($listvalue1);$i++)
    {
    	$idc   = intval($listid[$i]);
    	$order = intval($listorder[$i]);
    	$vl1   = trim($listvalue1[$i]);
    	$vl2   = trim($listvalue2[$i]);    
    	$value = ($Type=="Price")? $vl1."-".$vl2 : $vl1;
        
    	if(empty($vl1) || (empty($vl2) && $Type=="Price" && $vl1!=-1))continue;
    	
    	if($vl1==-1 && $idc>0)
        {
            $sql_delete="delete from tblproduct_filter where iId=$idc";
            $dbsql->query($sql_delete);
        }else if($idc==0)
    	{
    		$sql_insert="insert into tblproduct_filter 
    				(iCategoryId,iParentId,cName,cValue,iOrder) 
    		values($idP,$id,'$value','',$order)";
    		
    		$dbsql->query($sql_insert);
    	}else
    	{
    		$sql_update="update tblproduct_filter set
    					cName='$value',
    					iOrder='$order'
    					where iId=$idc";
    					
    		$dbsql->query($sql_update);
    	}
    }
}

$msg = "Save Filter Success !";
$page = "?module=$module&opt=filter&idP=$idP";
page_transfer($msg,$page);
?>